﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ExtractLocBamlCsv
{
    class Program
    {
        static readonly string[] _ValidTypes = {
                                            "Title",
                                            "ToolTip",
                                            "$Content",
                                            "Header",
                                            "Content",
                                            "Text",
                                            "Label"
                                          };
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("ExtractLocBamlCsv V 1.00");
                Console.WriteLine("Usage: ExtractLocBamlCsv <translated csv> <new csv>");
                Console.WriteLine("  <translated csv> CSV translated in the new language.");
                return;
            }

            Console.WriteLine(string.Format("Extract {0}", args[0]));
            try
            {
                ParseOriginal(args[0]);
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

        }

        // Test if a line contains a language part
        // - The columns 3 and 4 must be 'true'
        // - column 6 is filled but doesn't start with a # (this is a link)
        // - Contains a type of data we want to translate.
        // - Or we have an image source.
        private static bool IsTranslatableLine(string[] parts)
        {
            string[] names = parts[1].Split(new char[] { '.' });
            string lastName = names[names.Length - 1];

            return (((string.Compare(parts[3], "true", true) == 0) &&
                        (string.Compare(parts[4], "true", true) == 0) &&
                        (string.IsNullOrEmpty(parts[6]) == false) &&
                        (parts[6][0] != '#') &&
                        (_ValidTypes.Contains(lastName) == true)));
        }


        /// <summary>
        /// Extract the translated text parts from the last time.
        /// Place them in a dictionary for later use.
        /// </summary>
        /// <param name="fileOriginal">Current translated file (LocBaml CSV file)</param>
        /// <returns>Dictionary of text parts in the correct language.</returns>
        private static void ParseOriginal(string fileOriginal)
        {
            string fileNew = "trans.csv";
            using (StreamWriter sw = new StreamWriter(fileNew, false, Encoding.GetEncoding("UTF-8")))
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(fileOriginal))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        // Split the line into 7 parts and check if it's translatable.
                        string[] parts = line.Split(new char[] { ',' }, 7);
                        if ((parts.Length == 7) && (IsTranslatableLine(parts) == true))
                        {
                            sw.WriteLine(line);
                        }
                    }
                }
            }
        }
    }
}
